package com.chenjiahao.kaiyanwan.logic.dao

import androidx.lifecycle.LiveData
import androidx.lifecycle.liveData
import com.chenjiahao.kaiyanwan.logic.network.ServiceNetwork
import com.hazz.kotlinmvp.mvp.model.bean.HomeBean
import kotlinx.coroutines.Dispatchers
import java.lang.Exception
import java.lang.RuntimeException
import kotlin.coroutines.CoroutineContext


//仓库层
object Repository {
    //将liveData改为fire
    fun getFirstHomeData(num: Int) = fire(Dispatchers.IO) {
        val data = ServiceNetwork.getFirstHomeData(num)
        if (data != null) {
            val homeBean = data
            Result.success(homeBean)
        } else {
            Result.failure(RuntimeException("response status is ${data}"))
        }

    }

    fun getMoreHomeData(url: String) = fire(Dispatchers.IO) {
        val data = ServiceNetwork.getMoreHomeData(url)
        if (data != null) {
            val homeBean = data
            Result.success(homeBean)
        } else {
            Result.failure(RuntimeException("response status is ${data}"))
        }
    }

    fun getRelatedData(id: Long) = fire(Dispatchers.IO) {
        val data = ServiceNetwork.getRelatedData(id)
        if (data != null) {
            val homeBean = data
            Result.success(homeBean)
        } else {
            Result.failure(RuntimeException("response status is ${data}"))
        }
    }

    fun getSearchData(string: String) = fire(Dispatchers.IO) {
        val data = ServiceNetwork.getSearchData(string)
        if (data != null) {
            val homeBean = data
            Result.success(homeBean)
        } else {
            Result.failure(RuntimeException("response status is ${data}"))
        }
    }

    fun getIssueData(string: String) = fire(Dispatchers.IO) {
        val data = ServiceNetwork.getIssueData(string)
        if (data != null) {
            val homeBean = data
            Result.success(homeBean)
        } else {
            Result.failure(RuntimeException("response status is ${data}"))
        }
    }

    fun getHotWord() = fire(Dispatchers.IO) {
        val data = ServiceNetwork.getHotWord()
        if (data != null) {
            val homeBean = data
            Result.success(homeBean)
        } else {
            Result.failure(RuntimeException("response status is ${data}"))
        }
    }

    fun getCategory() = fire(Dispatchers.IO) {
        val data = ServiceNetwork.getCategory()
        if (data != null) {
            val homeBean = data
            Result.success(homeBean)
        } else {
            Result.failure(RuntimeException("response status is ${data}"))
        }
    }

    fun getRankList() = fire(Dispatchers.IO) {
        val data = ServiceNetwork.getRankList()
        if (data != null) {
            val homeBean = data
            Result.success(homeBean)
        } else {
            Result.failure(RuntimeException("response status is ${data}"))
        }
    }

    fun getFollowInfo() = fire(Dispatchers.IO) {
        val data = ServiceNetwork.getFollowInfo()
        if (data != null) {
            val homeBean = data
            Result.success(homeBean)
        } else {
            Result.failure(RuntimeException("response status is ${data}"))
        }
    }

    fun getCategoryDetailList(id: Long) = fire(Dispatchers.IO) {
        val data = ServiceNetwork.getCategoryDetailList(id)
        if (data != null) {
            val homeBean = data
            Result.success(homeBean)
        } else {
            Result.failure(RuntimeException("response status is ${data}"))
        }
    }

    private fun <T> fire(context: CoroutineContext, block: suspend () -> Result<T>) =
        liveData<Result<T>>(context) {
            val result = try {
                block()
            } catch (e: Exception) {
                Result.failure<T>(e)
            }
            emit(result)
        }

}